package com.projet.whattowatch.activity;

import com.projet.whattowatch.R;
import com.projet.whattowatch.objet.Avis;
import com.projet.whattowtach.utils.ImageLoader;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MovieFragment extends Fragment
{
	Button affiche;
	TextView titre;
	TextView duree;
	TextView dispo3d;
	Button wantToWath;
	ListView genreList;
	RatingBar ratingBar;
	int i = 0;
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.movie_fragment_layout, container, false);
        
        int loader = R.drawable.defaut_movie_img;
		affiche = (Button)rootView.findViewById(R.id.afficheFilm);
		
        String image_url = MovieDetailActivity.film.getImageUrl();
        ImageLoader imgLoader = new ImageLoader(getActivity().getApplicationContext());
        imgLoader.DisplayImage(image_url, loader, affiche);
        
        titre = (TextView)rootView.findViewById(R.id.titreFilm);
        titre.setText(MovieDetailActivity.film.getNom() + " (" + MovieDetailActivity.film.getDate() + ")");
        
        duree = (TextView)rootView.findViewById(R.id.dureeFilm);
        duree.setText(String.valueOf(MovieDetailActivity.film.getDuree()) + " min   -   " + MovieDetailActivity.film.getIs3DDispo());
        
        wantToWath = (Button) rootView.findViewById(R.id.wantToWatchButton);
        wantToWath.setOnClickListener(wantToWathListener);
        
        ratingBar = (RatingBar) rootView.findViewById(R.id.ratingBar);
        
        if (i == 0)
        {
	        ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
	        {
	            @Override
				public void onRatingChanged(final RatingBar ratingBar, final float rating,  boolean fromUser)
	            {
	            	LayoutInflater factory = getActivity().getLayoutInflater();
	                View alertDialogView = factory.inflate(R.layout.alert_dialog_rating, null);
	
	                AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
	         
	                //On affecte la vue personnalisé que l'on a crée à notre AlertDialog
	                alertDialog.setView(alertDialogView);
	                
	                TextView title = (TextView) alertDialogView.findViewById(R.id.alertDialogTextView);
	                final EditText commentText = (EditText) alertDialogView.findViewById(R.id.alertDialogEditText);
	                title.setText("Voulez-vous laisser un commentaire ?");
	         
	                alertDialog.setTitle("Merci pour votre avis !");
	
	                alertDialog.setNeutralButton("Terminer", new DialogInterface.OnClickListener() 
	                {
	                    @Override
						public void onClick(DialogInterface dialog, int which) 
	                    {
	                    	Avis avis = new Avis(ConnectedActivity.mUtilisateur.getPrenom(), rating, commentText.getText().toString());
	                    	MovieDetailActivity.film.addAvis(avis);
	                    	MainActivity.app_db.addAvis(ConnectedActivity.mUtilisateur, MovieDetailActivity.film, rating, commentText.getText().toString());
	                    	Toast.makeText(getActivity().getApplicationContext(),"Nous avons bien enregistré votre avis.",Toast.LENGTH_LONG).show();
	            	        i=1;
	            	        ratingBar.setIsIndicator(true);
	                    	ratingBar.setEnabled(false);
	                    }
	                });
	                
	                alertDialog.show();
	            }
	        });
        }else
        {
        	ratingBar.setIsIndicator(true);
        	ratingBar.setEnabled(false);
        }

        genreList = (ListView)rootView.findViewById(R.id.listViewGenre);
        
        ArrayAdapter<String> genreAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), 
        		R.layout.custom_textview, MovieDetailActivity.film.getListGenre());
        
        genreList.setAdapter(genreAdapter);
        
        return rootView;
    }
    
    public OnClickListener wantToWathListener = new OnClickListener()
    {
		@Override
		public void onClick(View arg0)
		{
			ConnectedActivity.mUtilisateur.addFilmAVoir(MovieDetailActivity.film);
			MainActivity.app_db.addFilmAVoir(ConnectedActivity.mUtilisateur, MovieDetailActivity.film);
			Toast.makeText(getActivity().getApplicationContext(), "Film ajouté à vos favoris", Toast.LENGTH_SHORT).show();
		}
    };
    
    public OnTouchListener ratingBarListener = new OnTouchListener()
    {
		@Override
		public boolean onTouch(View v, MotionEvent event)
		{
            LayoutInflater factory = getActivity().getLayoutInflater();
            View alertDialogView = factory.inflate(R.layout.alert_dialog_rating, null);

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
     
            //On affecte la vue personnalisé que l'on a crée à notre AlertDialog
            alertDialog.setView(alertDialogView);
            
            TextView title = (TextView) alertDialogView.findViewById(R.id.alertDialogTextView);
            final EditText commentText = (EditText) alertDialogView.findViewById(R.id.alertDialogEditText);
            title.setText("Voulez-vous laisser un commentaire ?");
     
            alertDialog.setTitle("Merci pour votre avis !");

            alertDialog.setNeutralButton("Terminer", new DialogInterface.OnClickListener() 
            {
                @Override
				public void onClick(DialogInterface dialog, int which) 
                {
                	Avis avis = new Avis(ConnectedActivity.mUtilisateur.getPrenom(), ratingBar.getRating(), commentText.getText().toString());
                	MovieDetailActivity.film.addAvis(avis);
                	ratingBar.setIsIndicator(true);
                	ratingBar.setEnabled(false);
                	Toast.makeText(getActivity().getApplicationContext(),"Nous avons bien enregistré votre avis.",Toast.LENGTH_LONG).show();
                }
            });
            
            alertDialog.show();
			return false;
		}
    };
}